//
//  HRNavigationController.swift
//  微博
//
//  Created by 众力金融 on 2017/1/5.
//  Copyright © 2017年 众力金融. All rights reserved.
//

import UIKit

class HRNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的 NavigationBar
        navigationBar.isHidden = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    
   //重写push的方法 所有的push的动作都会调用此方法
    //viewController 是被 push 的控制器 设置他的左侧的按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
       
        
        if childViewControllers.count > 0 {
            
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            
            if let vc = viewController as? HRBaseViewController {
                
                var title = "返回"
                
                //判断控制器的级数
                if childViewControllers.count == 1 {
                    //title 显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                //取出自定义 navitem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack: true)
            }
        }
        
         super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }
}
